<?php
namespace addons\comments;
use addons\comments\model\CommentsPostModel;

class Comments extends \think\Addons
{
    //是否有后台
    public $hasAdmin = 1;
    // 该插件的基础信息
    public $info = [
        //插件英文名，改成你的插件英文就行了
        'name'        => 'Comments',
        //插件名称
        'title'       => '评论插件',
        //插件描述
        'description' => '评论插件',
        //插件，1:开启;0:禁用
        'status'      => 1,
        //插件作者
        'author'      => '辰一',
        //版本号
        'version'     => '1.0',
        //演示地址
        'demo_url'    => 'http://lve123.com',
        //作者网站链接
        'author_url'  => 'http://lve123.com',
        //是否有后台 0 没有，1有
        'has_admin'   => 1
    ];
    /**
     * 插件安装方法
     * @return bool
     */
    public function install()
    {
        return true;
    }

    /**
     * 插件卸载方法
     * @return bool
     */
    public function uninstall()
    {
        return true;
    }

    public function commnentsView($param)
    {
        $commentList = [];
        if(isset($param['topic_id']))
        {
            $commentList = CommentsPostModel::where("topic_type",1)
                ->where("topic_id",$param['topic_id'])
                ->alias("a")
                ->leftJoin("cp_admin b","b.id=a.from_uid")
                ->leftJoin("cp_admin c","c.id=a.to_uid")
                ->where("a.is_status",1)
                ->field("a.from_uid,a.id,a.content,a.create_time,a.update_time")
                ->field("b.nickname from_nickname,b.id from_uid,b.username from_username,b.avatar from_avatar")
                ->field("c.nickname touser_nickname,c.id touser_uid,c.username touser_username,c.avatar touser_avatar")
                ->paginate($this->request->param("limit",15,'intval'));
        }
        //参与评论人数
        $commetUserCount =  (new CommentsPostModel())->where("topic_type",1)
            ->where("topic_id",$param['topic_id'])
            ->group("from_uid")
            ->count("id");
        $this->assign([
            "param"       => $param,
            "comment" => $commentList,
            "commetUserCount" => $commetUserCount,
        ]);
        return $this->fetch("comments");
    }


}